package com.cqut.dao.base;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Entity;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

/**
 * 基础Dao
 * 
 * @author YuanXiang
 * 
 */
public class BaseDao implements DAO {
	protected HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	/**
	 * 按主键删除对象
	 */
	public <T> void delete(Class<T> entity, Serializable entityId) {
		String entityName = this.getEntityName(entity);
		hibernateTemplate.delete(entityName, entityId);
	}

	/**
	 * 按多个主键删除对象
	 */
	public <T> void delete(Class<T> entity, Serializable[] entityIds) {
		for (int i = 0; i < entityIds.length; i++) {
			this.delete(entity, entityIds[i]);
		}

	}

	/**
	 * 执行更新hql语句
	 */
	public Integer executeUpdate(String hql) {
		return hibernateTemplate.bulkUpdate(hql);
	}

	/**
	 * 执行查询hql语句
	 */
	public <T> List<T> executeQuery(String hql) {
		return hibernateTemplate.find(hql);
	}

	/**
	 * 按主键得到对象
	 */
	public <T> T loadById(Class<T> entity, Serializable entityId) {
		return (T) hibernateTemplate.load(entity, entityId);
	}

	/**
	 * 保存单个对象
	 */
	public void save(Object entity) {
		hibernateTemplate.save(entity);
	}

	/**
	 * 保存多个对象
	 */
	public void saves(Object[] entitys) {
		for (int i = 0; i < entitys.length; i++) {
			this.save(entitys[i]);
		}

	}

	/**
	 * 更新单个对象
	 */
	public void update(Object entity) {
		hibernateTemplate.update(entity);

	}

	/**
	 * 更新多个对象
	 */
	public void updates(Object[] entitys) {
		for (int i = 0; i < entitys.length; i++) {
			this.update(entitys[i]);
		}
	}

	/**
	 * 获取entity的名称
	 * 
	 * @param <T>
	 * @param entity
	 * @return
	 */
	protected <T> String getEntityName(Class<T> entity) {
		String entityName = null;
		Entity en = entity.getAnnotation(Entity.class);
		if (en != null && !"".equals(en.name())) {
			entityName = en.name();
		} else {
			entityName = entity.getSimpleName();
		}
		return entityName;
	}

	public <T> List<T> find(String queryStirng, Long firstResult,
			Long maxResults) {
		return null;
	}

	public <T> List<T> find(String queryStirng,
			LinkedHashMap<String, Object> linkedHashMap, Long firstResult,
			Long maxResults) {
		return null;
	}

	public List findByExample(String entityName, Object exampleEntity) {
		return null;
	}

	/**
	 * 根据id查找对象（该对象不一定存在）
	 */
	public <T> T findById(Class<T> entity, Serializable entityId) {
		return (T) hibernateTemplate.get(entity, entityId);
	}

	public <T> T findOneBySql(Class<T> entity, String whereSql, Object[] params) {
		String entityName = this.getEntityName(entity);
		// 生成查询对象
		Query query = hibernateTemplate
				.getSessionFactory()
				.getCurrentSession()
				.createQuery(
						"select o from " + entityName + " o "
								+ (whereSql == null ? "" : "where " + whereSql));
		this.setParam(query, params);

		return (T) query.uniqueResult();
	}

	public Long getEntityCount(String hql) {
		return null;
	}

	public Long getEntityCount(String hql,
			LinkedHashMap<String, Object> linkedHashMap) {
		return null;
	}

	public <T> Long getSingleResult(Class<T> entity) {
		return null;
	}

	public void setParam(Query query, Object[] params) {
		if (params != null && params.length > 0) {
			for (int i = 0; i < params.length; i++) {
				query.setParameter(i, params[i]);
			}
		}
	}
}
